package com.financing.wap.web.controller.api;

import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.wap.common.Pager;
import com.financing.wap.common.ResultMap;
import com.financing.wap.dto.DstUserWeeklyDTO;
import com.financing.wap.service.IDstUserWeeklyService;
import com.financing.wap.util.DTOdateSetUtil;
import com.financing.wap.web.controller.BaseController;

@Controller("dstUserWeeklyController")
@RequestMapping("/api/dstUserWeekly")
public class DstUserWeeklyController extends BaseController  { 

	private Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private IDstUserWeeklyService dstUserWeeklyService;
	
	@RequestMapping("/listDstUserWeekly")
	@ResponseBody
	public ResultMap list(HttpServletRequest request, DstUserWeeklyDTO dstUserWeekly,Pager pager){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query dstUserWeekly list begin .......");
			
			dstUserWeekly.setBegin(pager.getStartIndex());
			dstUserWeekly.setPageSize(pager.getPageSize());
			
			int count = dstUserWeeklyService.queryListCount(dstUserWeekly);
			
			pager.setTotalCount(count);
			
			List<DstUserWeeklyDTO> dstUserWeeklyList = null;
			
			if(count > 0){
				dstUserWeeklyList = dstUserWeeklyService.queryList(dstUserWeekly);
			}else{
				dstUserWeeklyList = Collections.emptyList();
			}
			result.setAttribute("pager", pager);
			result.setAttribute("list", dstUserWeeklyList);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query dstUserWeekly list end .......");
		}catch(Exception e){
			log.error("query dstUserWeekly error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/addDstUserWeekly")
	@ResponseBody
	public ResultMap add(HttpServletRequest request, DstUserWeeklyDTO dstUserWeekly){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("add dstUserWeekly  begin .......");
			
			DTOdateSetUtil.setCreateInfo(dstUserWeekly);
			DTOdateSetUtil.setUpdateInfo(dstUserWeekly);
			
			int id = dstUserWeeklyService.insert(dstUserWeekly);
			result.setAttribute("id", id);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("add dstUserWeekly list end .......");
		}catch(Exception e){
			log.error("add dstUserWeekly error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	
	@RequestMapping("/detailDstUserWeekly")
	@ResponseBody
	public ResultMap detail(HttpServletRequest request, DstUserWeeklyDTO dstUserWeekly){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query detail dstUserWeekly  begin .......");
			
			Integer id = dstUserWeekly.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			DstUserWeeklyDTO dto = dstUserWeeklyService.loadById(id);
			
			result.setAttribute("dto",dto);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query detail dstUserWeekly  end .......");
		}catch(Exception e){
			log.error("query detail dstUserWeekly error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/deleteDstUserWeekly")
	@ResponseBody
	public ResultMap delete(HttpServletRequest request, DstUserWeeklyDTO dstUserWeekly){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("delete dstUserWeekly  begin .......");
			
			Integer id = dstUserWeekly.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			dstUserWeeklyService.delete(id);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("delete dstUserWeekly  end .......");
		}catch(Exception e){
			log.error("delete dstUserWeekly error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/updateDstUserWeekly")
	@ResponseBody
	public ResultMap update(HttpServletRequest request, DstUserWeeklyDTO dstUserWeekly){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("update dstUserWeekly  begin .......");
			
			Integer id = dstUserWeekly.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			DTOdateSetUtil.setUpdateInfo(dstUserWeekly);
			
			dstUserWeeklyService.update(dstUserWeekly);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("update dstUserWeekly list end .......");
		}catch(Exception e){
			log.error("update dstUserWeekly error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/updateForceDstUserWeekly")
	@ResponseBody
	public ResultMap updateForce(HttpServletRequest request, DstUserWeeklyDTO dstUserWeekly){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("updateForce dstUserWeekly  begin .......");
			
			Integer id = dstUserWeekly.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			DTOdateSetUtil.setUpdateInfo(dstUserWeekly);
			
			dstUserWeeklyService.updateForce(dstUserWeekly);
			
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("updateForce dstUserWeekly list end .......");
		}catch(Exception e){
			log.error("updateForce dstUserWeekly error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
}
